.\"Crystal Programming Language
.Dd
.Dt CRYSTAL(1) "" "Crystal Compiler Command Line Reference Guide"
.\".Dt CRYSTAL 1
.Os UNIX
.Sh NAME
.Nm crystal
.Nd Compiler for the Crystal language.
.Sh SYNOPSIS
.Nm
command
.Op switches
programfile
--
.Op arguments
.Sh DESCRIPTION
Crystal is a statically type-checked programming language. It was created with the beauty of Ruby and the performance of C in mind.
.Sh USAGE
You can compile and run a program by invoking the compiler with a single filename:
.Bd -offset indent-two
.Nm
some_program.cr
.Ed

Crystal files usually end with the .cr extension, though this is not mandatory.

Alternatively you can use the run command:
.Bd -offset indent-two
.Nm
run
some_program.cr
.Ed

To create an executable use the build command:
.Bd -offset indent-two
.Nm
build
some_program.cr
.Ed

This will create an executable named "some_program".

Note that by default the generated executables are not fully optimized.
To turn optimizations on, use the --release flag:
.Bd -offset indent-two
.Nm
build
--release
some_program.cr
.Ed

Make sure to always use --release for production-ready executables and when performing benchmarks.

The optimizations are not turned on by default because the compile times are much faster without them and the performance of the program is still pretty good without them, so it allows to use the crystal command almost to be used as if it was an interpreter.

.Bl -tag -width "12345678" -compact
.Pp
.Sh OPTIONS
The crystal command accepts the following options

.Bl -tag -width "12345678" -compact
.Pp
.It
.Cm init
TYPE
.Op DIR | NAME DIR
.Pp
Generates a new Crystal project.
.Pp
TYPE is one of:
.Bl -tag -width "12345678" -compact
.Pp
.It Sy lib
Creates a library skeleton
.It Sy app
Creates an application skeleton
.El

This initializes the lib/app project folder as a git repository, with a license file, a README file, a shard.yml for use with shards (the Crystal dependency manager), a .gitignore file, and src and spec folders.
.Bd -literal -offset
DIR  - directory where project will be generated
.Pp
NAME - name of project to be generated (default: basename of DIR)
.Ed
.Pp
Options:
.Bl -tag -width "12345678" -compact
.Pp
.It Fl f, Fl -force
Force overwrite existing files.
.It Fl s, Fl -skip-existing
Skip existing files.
.El

.Pp
.It
.Cm build
.Op options
.Op programfile
.Op --
.Op arguments
.Pp
Compile program.
.Pp
Options:
.Bl -tag -width "12345678" -compact
.Pp
.It Fl -cross-compile
Generate an object file for cross compilation and prints the command to build the executable.
The object file should be copied to the target system and the printed command should be executed there. This flag mainly exists for porting the compiler to new platforms, where possible run the compiler on the target platform directly.
.It Fl d, Fl -debug
Generate the output with symbolic debug symbols.
These are read when debugging the built program with tools like lldb, gdb, valgrind etc. and provide mappings to the original source code for those tools.
.It Fl -no-debug
Generate the output without any symbolic debug symbols.
.It Fl -lto Ar FLAG
Use ThinLTO --lto=thin.
.It Fl D Ar FLAG, Fl -define Ar FLAG
Define a compile-time flag. This is useful to conditionally define types, methods, or commands based on flags available at compile time. The default flags are from the target triple given with --target-triple or the hosts default, if none is given.
.It Fl -emit Op asm|llvm-bc|llvm-ir|obj
Comma separated list of types of output for the compiler to emit. You can use this to see the generated LLVM IR, LLVM bitcode, assembly, and object files.
.It Fl f Ar text|json, Fl -format Ar text|json
Format of output. Defaults to text. The json format can be used to get a more parser-friendly output.
.It Fl -error-trace
Show full error trace.
.It Fl -ll
Dump LLVM assembly file to output directory.
.It Fl -link-flags Ar FLAGS
Pass additional flags to the linker. Though you can specify those flags on the source code, this is useful for passing environment specific information directly to the linker, like non-standard library paths or names. For more information on specifying linker flags on source, you can read the "C bindings" section of the documentation available on the official web site.
.It Fl -mcpu Ar CPU
Specify a specific CPU to generate code for. This will pass a -mcpu flag to LLVM, and is only intended to be used for cross-compilation. For a list of available CPUs, invoke "llvm-as < /dev/null | llc -march=xyz -mcpu=help".
Passing --mcpu native will pass the host CPU name to tune performance for the host.
.It Fl -mattr Ar CPU
Override or control specific attributes of the target, such as whether SIMD operations are enabled or not. The default set of attributes is set by the current CPU. This will pass a -mattr flag to LLVM, and is only intended to be used for cross-compilation. For a list of available attributes, invoke "llvm-as < /dev/null | llc -march=xyz -mattr=help".
.It Fl -mcmodel default|kernel|small|medium|large
Specifies a specific code model to generate code for. This will pass a --code-model flag to LLVM.
.It Fl -no-color
Disable colored output.
.It Fl -no-codegen
Don't do code generation, just parse the file.
.It Fl o
Specify filename of output.
.It Fl -prelude
Specify prelude to use. The default one initializes the garbage collector. You can also use --prelude=empty to use no preludes. This can be useful for checking code generation for a specific source code file.
.It Fl -release
Turn on optimizations for the generated code, which are disabled by default.
.It Fl -error-trace
Show full stack trace. Disabled by default, as the full trace usually makes error messages less readable and not always deliver relevant information.
.It Fl s, -stats
Print statistics about the different compiler stages for the current build. Output time and used memory for each compiler process.
.It Fl p, -progress
Print statistics about the progress for the current build.
.It Fl t, -time
Print statistics about the execution time.
.It Fl -single-module
Generate a single LLVM module.
.It Fl -threads Ar NUM
Maximum number of threads to use for code generation. The default is 8 threads.
.It Fl -target Ar TRIPLE
Enable target triple; intended to use for cross-compilation. See llvm documentation for more information about target triple.
.It Fl -verbose
Display the commands executed by the system.
.It Fl -static
Create a statically linked executable.
.It Fl -stdin-filename Ar FILENAME
Source file name to be read from STDIN.
.El

.Pp
.It
.Cm docs
.Pp
Generate documentation from comments using a subset of markdown. The output is saved in html format on the created docs/ folder. More information about documentation conventions can be found at https://crystal-lang.org/docs/conventions/documenting_code.html.
.Pp
Options:
.Bl -tag -width "12345678" -compact
.Pp
.It Fl -project-name Ar NAME
Set the project name. The default value is extracted from shard.yml if available.

In case no default can be found, this option is mandatory.
.It Fl -project-version Ar VERSION
Set the project version. The default value is extracted from current git commit or shard.yml if available.

In case no default can be found, this option is mandatory.
.It Fl -json-config-url Ar URL
Set the URL pointing to a config file (used for discovering versions).
.It Fl -source-refname Ar REFNAME
Set source refname (e.g. git tag, commit hash). The default value is extracted from current git commit if available.

If this option is missing and can't be automatically determined, the generator can't produce source code links.
.It Fl -source-url-pattern Ar URL
Set URL pattern for source code links. The default value is extracted from git remotes ("origin" or first one) if available and the provider's URL pattern is recognized.

.Pp
Supported replacement tags:
.Pp
.Bl -tag -width "%{refname}" -compact
.It Sy %{refname}
commit reference
.It Sy %{path}
path to source file inside the repository
.It Sy %{filename}
basename of the source file
.It Sy %{line}
line number
.El
.Pp
If this option is missing and can't be automatically determined, the generator can't produce source code links.
.It Fl o Ar DIR, Fl -output Ar DIR
Set the output directory (default: ./docs).
.It Fl b Ar URL, Fl -canonical-base-url Ar URL
Indicate the preferred URL with rel="canonical" link element.
.It Fl b Ar URL, Fl -sitemap-base-url Ar URL
Set the sitemap base URL. Sitemap will only be generated when this option is set.
.It Fl -sitemap-priority Ar PRIO
Set the priority assigned to sitemap entries (default: 1.0).
.It Fl -sitemap-changefreq Ar FREQ
Set the changefreq assigned to sitemap entries (default: never).
.El
.Pp
.It
.Cm env
.Op variables
.Pp
Print Crystal-specific environment variables in a format compatible with shell scripts. If one or more variable names are given as arguments, it prints only the value of each named variable on its own line.
.Pp
Variables:
.Bl -tag -width "12345678" -compact
.Pp
.It
.It Sy CRYSTAL_CACHE_DIR
Please see
.Sm "ENVIRONMENT VARIABLES".
.Pp
.It
.It Sy CRYSTAL_LIBRARY_PATH
Please see
.Sm "ENVIRONMENT VARIABLES".
.Pp
.It
.It Sy CRYSTAL_PATH
Please see
.Sm "ENVIRONMENT VARIABLES".
.Pp
.It
.It Sy CRYSTAL_VERSION
Contains Crystal version.
.El
.Pp
.It
.Cm eval
.Op options
.Op source
.Pp
Evaluate code from arguments or, if no arguments are passed, from the standard input. Useful for experiments.
.Pp
Options:
.Bl -tag -width "12345678" -compact
.Pp
.It Fl d, Fl -debug
Generate the output with symbolic debug symbols.
These are read when debugging the built program with tools like lldb, gdb, valgrind etc. and provide mappings to the original source code for those tools.
.It Fl -no-debug
Generate the output without any symbolic debug symbols.
.It Fl D Ar FLAG, Fl -define Ar FLAG
Define a compile-time flag. This is useful to conditionally define types, methods, or commands based on flags available at compile time. The default flags are from the target triple given with --target-triple or the hosts default, if none is given.
.It Fl -error-trace
Show full error trace.
.It Fl -release
Turn on optimizations for the generated code, which are disabled by default.
.It Fl s, -stats
Print statistics about the different compiler stages for the current build. Output time and used memory for each compiler process.
.It Fl p, -progress
Print statistics about the progress for the current build.
.It Fl t, -time
Print statistics about the execution time.
.It Fl -no-color
Disable colored output.
.El
.Pp
.It
.Cm play
.Op options
.Op file
.Pp
Starts the crystal playground server on port 8080, by default.
.Pp
Options:
.Bl -tag -width "12345678" -compact
.Pp
.It Fl p Ar PORT, Fl -port Ar PORT
Run the playground on the specified port. Default is 8080.
.It Fl b Ar HOST, Fl -binding Ar HOST
Bind the playground to the specified IP.
.It Fl v, Fl -verbose
Display detailed information of the executed code.
.El
.Pp
.It
.Cm run
.Op options
.Op programfile
.Op --
.Op arguments
.Pp
The default command. Compile and run program.
.Pp
Options:
Same as the build options.
.Pp
.It
.Cm spec
.Op options
.Op files
.Pp
Compile and run specs (in spec directory).
.Pp
Options:
.Bl -tag -width "12345678" -compact
.Pp
.It Fl d, Fl -debug
Generate the output with symbolic debug symbols.
These are read when debugging the built program with tools like lldb, gdb, valgrind etc. and provide mappings to the original source code for those tools.
.It Fl -no-debug
Generate the output without any symbolic debug symbols.
.It Fl D Ar FLAG, Fl -define Ar FLAG
Define a compile-time flag. This is useful to conditionally define types, methods, or commands based on flags available at compile time. The default flags are from the target triple given with --target-triple or the hosts default, if none is given.
.It Fl -error-trace
Show full error trace.
.It Fl -release
Turn on optimizations for the generated code, which are disabled by default.
.It Fl s, -stats
Print statistics about the different compiler stages for the current build. Output time and used memory for each compiler process.
.It Fl p, -progress
Print statistics about the progress for the current build.
.It Fl t, -time
Print statistics about the execution time.
.It Fl -no-color
Disable colored output.
.El
.Pp
.It
.Cm tool
.Op tool
.Op switches
.Op programfile
.Op --
.Op arguments
.Pp
Run a tool. The available tools are: context, format, hierarchy, implementations, and types.
.Pp
Tools:
.Bl -tag -offset indent
.It Cm context
Show context for given location.
.It Cm expand
Show macro expansion for given location.
.It Cm format
Format project, directories and/or files with the coding style used in the standard library. You can use the
.Fl -check
flag to check whether the formatter would make any changes.
.It Cm hierarchy
Show hierarchy of types from file. Also show class and struct members, with type and size. Types can be filtered with a regex by using the
.Fl e
flag.
.It Cm implementations
Show implementations for a given call. Use
.Fl -cursor
to specify the cursor position. The format for the cursor position is file:line:column.
.It Cm types
Show type of main variables of file.
.El
.Pp
.It Cm help, Fl -help, h
.Pp
Show help. Option --help or -h can also be added to each command for command-specific help.
.Pp
.It Cm version, Fl -version, v
.Pp
Show version.
.El
.
.Sh ENVIRONMENT VARIABLES
.Bl -tag -width "12345678" -compact
.Pp
.It
.It Sy CRYSTAL_CACHE_DIR
Defines path where Crystal caches partial compilation results for faster subsequent builds. This path is also used to temporarily store executables when Crystal programs are run with 'crystal run' rather than 'crystal build'.
.Pp
.It
.It Sy CRYSTAL_LIBRARY_PATH
Defines paths where Crystal searches for (binary) libraries. Multiple paths can be separated by ":".
These paths are passed to the linker as `-L` flags.
.Pp
The pattern '$ORIGIN' at the start of the path expands to the directory where the compiler binary is located. For example, '$ORIGIN/../lib/crystal' resolves the standard library path relative to the compiler location in a generic way, independent of the absolute paths (assuming the relative location is correct).
.Pp
.It
.It Sy CRYSTAL_PATH
Defines paths where Crystal searches for required source files. Multiple paths can be separated by ":".
.Pp
The pattern '$ORIGIN' at the start of the path expands to the directory where the compiler binary is located. For example, '$ORIGIN/../share/crystal/src' resolves the standard library path relative to the compiler location in a generic way, independent of the absolute paths (assuming the relative location is correct).
.Pp
.It
.It Sy CRYSTAL_OPTS
Defines options for the Crystal compiler to be used besides the command line arguments. The syntax is identical to the command line arguments. This is hany when using Crystal in build setups, for example 'CRYSTAL_OPTS=--debug make build'.
.El
.Sh SEE ALSO
.Fn shards 1
.Bl -hang -compact -width "https://github.com/crystal-lang/crystal/1234"
.It https://crystal-lang.org/
The official web site.
.It https://github.com/crystal-lang/crystal
Official Repository.
.El
